package com.feng.oauth2jwt.oauth.service.impl;


import com.feng.oauth2jwt.oauth.domain.dto.OauthClientDetailsDto;
import com.feng.oauth2jwt.oauth.domain.dto.UserFormDto;
import com.feng.oauth2jwt.oauth.domain.userUrl.UserUrls;
import com.feng.oauth2jwt.oauth.service.OauthService;
import com.feng.oauth2jwt.oauth.service.UserOperateService;
import com.feng.oauth2jwt.oauth.service.UserService;
import com.feng.oauth2jwt.oauth.service.UserUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("userOperateService")
public class UserOperateServiceImpl implements UserOperateService {
    @Autowired
    private OauthService oauthService;
    @Autowired
    private UserService ouathUserService;
    @Autowired
    private UserUrlService userUrlService;
    @Override
    @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
    public void saveSpreadUserOauthUser(String username,String password,String clientId,String clientSecret,String key,String phone,String email) {
        Date date = new Date();
        OauthClientDetailsDto dto = new OauthClientDetailsDto();
        dto.setClientId(clientId);
        dto.setClientSecret(clientSecret);
        dto.setResourceIds("default-resource");
        dto.setScope("read,write");
        dto.setAuthorizedGrantTypes("authorization_code,password,refresh_token,implicit");
        dto.setAuthorities("ROLE_CLIENT");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dto.setCreateTime(sdf.format(date));
        oauthService.registerClientDetails(dto);
        UserFormDto formDto = new UserFormDto();
        formDto.setGuid(key);
        formDto.setUsername(username);
        formDto.setClientId(clientId);
        
        formDto.setPassword(password);
        formDto.setEmail(email);
        formDto.setPhone(phone);
        formDto.setCreateTime(sdf.format(date));
        ouathUserService.saveUser(formDto);
        
        UserUrls userUrls = new UserUrls();
        userUrls.setUsername(username);
        userUrlService.saveAllUrls(userUrls);
    }
    @Override
    public void saveEnterpriseUserOauthUser(String username, String password, String clientId, String clientSecret, String key, String phone,
            String email) {
        Date date = new Date();
        OauthClientDetailsDto dto = new OauthClientDetailsDto();
        dto.setClientId(clientId);
        dto.setClientSecret(clientSecret);
        dto.setResourceIds("default-resource");
        dto.setScope("read,write");
        dto.setAuthorizedGrantTypes("authorization_code,password,refresh_token,implicit");
        dto.setAuthorities("ROLE_CLIENT");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dto.setCreateTime(sdf.format(date));
        oauthService.registerClientDetails(dto);
        UserFormDto formDto = new UserFormDto();
        formDto.setGuid(key);
        formDto.setUsername(username);
        formDto.setClientId(clientId);

        formDto.setPassword(password);
        formDto.setEmail(email);
        formDto.setPhone(phone);
        formDto.setCreateTime(sdf.format(date));
        ouathUserService.saveUser(formDto);

    }
    @Override
    public void saveSelfDefinitionUserOauthUser(String username, String password, String clientId, String clientSecret, String key, List<String> urlList) {
        Date date = new Date();
        OauthClientDetailsDto dto = new OauthClientDetailsDto();
        dto.setClientId(clientId);
        dto.setClientSecret(clientSecret);
        dto.setResourceIds("default-resource");
        dto.setScope("read,write");
        dto.setAuthorizedGrantTypes("authorization_code,password,refresh_token,implicit");
        dto.setAuthorities("ROLE_CLIENT");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dto.setCreateTime(sdf.format(date));
        oauthService.registerClientDetails(dto);
        UserFormDto formDto = new UserFormDto();
        formDto.setGuid(key);
        formDto.setUsername(username);
        formDto.setClientId(clientId);
        
        formDto.setPassword(password);
//        formDto.setEmail(email);
//        formDto.setPhone(phone);
        formDto.setCreateTime(sdf.format(date));
        ouathUserService.saveUser(formDto);
        
        List<UserUrls> userUrlList = new ArrayList<UserUrls>();
        for (String url : urlList) {
            UserUrls u = new UserUrls();
            u.setUsername(username);
            u.setWebUrl(url);
            userUrlList.add(u);
        }
        userUrlService.saveSelfUrls(userUrlList);
    }
    
}
